//290.单词规律
//https://leetcode.cn/problems/word-pattern/?envType=study-plan-v2&envId=top-interview-150
class Solution {
public:
    bool wordPattern(string pattern, string s) {
        unordered_map<string, char> hashp;
        unordered_map<char, string> hashs;
        int np = pattern.size();
        int ns = s.size();
        int p1 = 0, p2 = 0;
        while (p1 < np && p2 < ns)
        {
            char c = pattern[p1];
            auto p = s.find(' ', p2);
            string tmp;
            if (p == string::npos) tmp = s.substr(p2);
            else tmp = s.substr(p2, p-p2);
            if (hashp.count(tmp) == 1 && hashp[tmp] != c) return false;
            if (hashp.count(tmp) == 0) hashp[tmp] = c;
            if (hashs.count(c) == 1 && hashs[c] != tmp) return false;
            if (hashs.count(c) == 0) hashs[c] = tmp;
            ++p1;
            if (p != string::npos) p2 = p + 1;
            else p2 = ns;
        }
        if (p1 < np || p2 < ns) return false;
        return true;
    }
};